package com.yifi.system.controller;

import com.jfinal.core.Path;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.yifi.common.base.BaseController;
import com.yifi.common.utils.DateUtils;
import com.yifi.common.utils.DbUtils;
import com.yifi.common.utils.ToolSession;
import com.yifi.common.utils.ToolString;

import cn.hutool.core.util.IdUtil;

@Path(value = "/SysDictData", viewPath = "/")
public class SysDictDataController extends BaseController {

	public void index() {
		String type = getPara("type", "");
		set("type", type);
		render("system/dict/data/data.html");
	}

	public void getData() {
		String id = getPara("id", "");

		String sql = "select * from sys_dict_data where dict_code=?";
		Record record = Db.findFirst(sql, id);
		if (record == null) {
			record = new Record();
			record.set("status", "0");
		}
		renderJson(record);
	}

	public void list() {
		String dict_label = getPara("dict_label", "");
		String c_pro = ToolSession.getUser(this).getC_pro();
		String type = getPara("type", "");
		String sqlSelect = "select * ";

		String sqlExcept = " from sys_dict_data where dict_type='" + type + "' and c_pro='" + c_pro
				+ "' order by dict_sort ";
		renderJson(getDataTable(sqlSelect, sqlExcept));
	}

	public void add() {
		String dict_type = getPara("type", "");
		set("dict_type", dict_type);

		render("system/dict/data/edit.html");
	}

	public void edit() {
		String id = getPara("id", "");
		set("id", id);
		render("system/dict/data/edit.html");
	}

	public void save() {
		String c_pro = ToolSession.getUser(this).getC_pro();
		String dict_code = getPara("dict_code", "");
		String dict_value = getPara("dict_value", "");
		String dict_label = getPara("dict_label", "");
		String dict_type = getPara("dict_type", "");
		String status = getPara("status", "");
		String dict_sort = getPara("dict_sort", "1");
		String remark = getPara("remark", "");

		String sql = "select * from sys_dict_data where dict_code=?";
		Record record = Db.findFirst(sql, dict_code);
		boolean isedit = true;
		if (record == null) {
			isedit = false;
			record = new Record();
			record.set("dict_code", IdUtil.fastUUID());
			record.set("create_by", ToolSession.getUser(this).getCname());
			record.set("create_time", DateUtils.getNowDate());
		}
		record.set("dict_value", dict_value);
		record.set("dict_label", dict_label);
		record.set("dict_type", dict_type);
		record.set("status", status);
		record.set("dict_sort", dict_sort);
		record.set("remark", remark);
		record.set("c_pro", c_pro);
		if (DbUtils.save("sys_dict_data", "dict_code", record, isedit)) {
			renderJson(success("保存成功！"));
		} else {
			renderJson(error("保存失败！"));
		}
	}

	public void remove() {
		String ids = getPara("ids", "");
		ids = ToolString.getIdsToStr(ids);

		String sql = "delete from sys_dict_data where dict_code in (" + ids + ")";
		if (Db.update(sql) > 0) {
			renderJson(success("删除成功！"));
		} else {
			renderJson(error("删除失败！"));
		}

	}
}
